package com.vf.admin.miaodong.dao;

import com.vf.admin.common.db.BaseDao;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.*;
import com.vf.admin.utils.StringEx;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
 * 订单
 */
@Repository
public class AfterOrderDao extends BaseDao<AppOrder> {

    /**
     * 详情
     */
    public AppOrder get(Integer id) {
        if(id==null || id<=0) {
            return null;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_order where id = ? ");
        paramList.add(id);
        String sql = sbStr.toString();
        return this.queryObj(sql, paramList, AppOrder.class);
    }

    /**
     * 添加/修改
     */
    public void save(AppOrder appOrder) {
        if (appOrder!=null && appOrder.getId() != null && appOrder.getId() > 0) {
            this.update(appOrder);
        } else {
            appOrder.setId(this.add(appOrder));
        }
    }

    /**
     * 删除
     */
    public void del(Integer id) {
        if(id==null || id <= 0) {
            return;
        }
        String sql = "delete  from app_order where id = ?";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(id);
        this.del(sql, paramList);
    }


    /**
     * 综合查询列表
     * @param map
     * @param bean
     * @return
     */
    public List<AppOrder> findList(Map<String, Object> map, SplitPageBean bean) {
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("");
        sbStr.append(" SELECT a.* FROM app_order a where 1=1 ");
        if (StringEx.isNotNull(map.get("nick"))) {
            sbStr.append(" and a.nick like ? ");
            paramList.add("%" + map.get("nick") + "%");
        }
        if(StringEx.isNotNull(map.get("phone"))){
            sbStr.append(" and a.phone = ? ");
            paramList.add(map.get("phone"));
        }
        if(StringEx.isNotNull(map.get("type"))){
            sbStr.append(" and a.type = ? ");
            paramList.add(map.get("type"));
        }
        if(StringEx.isNotNull(map.get("status"))){
            sbStr.append(" and a.status = ? ");
            paramList.add(map.get("status"));
        }
        if(StringEx.isNotNull(map.get("ordernum"))){
            sbStr.append(" and a.ordernum = ? ");
            paramList.add(map.get("ordernum"));
        }
        if (!StringEx.isNull(map.get("startdate"))) {
            sbStr.append(" and a.createtime>=? ");
            paramList.add(map.get("startdate"));
        }
        if (!StringEx.isNull(map.get("enddate"))) {
            sbStr.append(" and a.createtime<=? ");
            paramList.add(map.get("enddate"));
        }
        sbStr.append(" order by a.createtime desc ");
        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppOrder.class, bean);
    }

    public List<AppMingxiChongzhi> total(Map<String, Object> params) {
        ArrayList<Object> paramList = new ArrayList<>();
        StringBuilder sbStr = new StringBuilder(" select * from app_order where 1=1 ");
        sbStr.append(" and status = 2 ");
//        if(params.containsKey("today") && StringEx.isNotNull(params.get("today"))) {
//            sbStr.append(" and to_days(createtime) = to_days(now())");
//        }
        if(params.containsKey("today") && StringEx.isNotNull(params.get("today"))) {
            sbStr.append(" and createtime like ? " );
            paramList.add("%" + params.get("today") + "%");
        }
        if(params.containsKey("alltoday") && StringEx.isNotNull(params.get("alltoday"))) {
            sbStr.append(" and createtime <= ? " );
            paramList.add(params.get("alltoday"));
        }

        String sql = sbStr.toString();
        return this.queryList(sql,paramList, AppMingxiChongzhi.class);

    }

    public List<AppOrder> getAppOrderV2(Map<String, Object> params) {
        ArrayList<Object> paramList = new ArrayList<>();
        StringBuilder sbStr = new StringBuilder("select * from app_order where 1=1 ");
        //默认已支付
        sbStr.append(" and status = 2 ");
        //今日
        if(params.containsKey("today") && StringEx.isNotNull(params.get("today"))) {
            sbStr.append(" and createtime like ? " );
            paramList.add("%" + params.get("today") + "%");
        }
        //本月
        if(params.containsKey("month") && StringEx.isNotNull(params.get("month"))) {
            sbStr.append(" and createtime like ? " );
            paramList.add("%" + params.get("month") + "%");
        }
        //总的
        if(params.containsKey("all") && StringEx.isNotNull(params.get("all"))) {
            sbStr.append(" and createtime <= ?  " );
            paramList.add(params.get("all"));
        }

        String sql = sbStr.toString();
        return this.queryList(sql,paramList, AppOrder.class);

    }
}
